﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MySql.Data.MySqlClient;
using FetchDataSystem.Model;

namespace FetchDataSystem.DAL
{
    public class EquipmentService
    {
        public static Equipment GetEquipmentByCondition(string cellId,string stationId,string equipTable,string equipName)
        {
            Equipment equipment = null;
            string condition = null;
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            if (cellId != null)
            {
                condition += " and cell_id=@cell_id";
                parameters.Add(new MySqlParameter("@cell_Id", cellId));
            }
            if (stationId != null)
            {
                condition += " and station_id=@station_id";
                parameters.Add(new MySqlParameter("@station_Id", stationId));
            }
            string sql = string.Format("select * from {0} where 1=1 {1}", equipTable, condition);
            try
            {
                using (MySqlDataReader dr = DBHelper.ExecuteReader(DBHelper.ConnectionString, CommandType.Text, sql, parameters.ToArray()))
                {
                    if (dr.Read())
                    {
                        equipment = new Equipment();
                        equipment.CellId = cellId;
                        equipment.StationId = dr["station_id"].ToString();
                        equipment.EquipmentName = equipName;
                        equipment.QrCode = dr["qrCode"].ToString();
                        equipment.Id = dr["id"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            return equipment;
        }

    }
}
